from django.shortcuts import render
from django.views import View
from xiaoyu_mall.apps.contents.utils import get_categories
from collections import OrderedDict
from xiaoyu_mall.apps.users.models import *

# Create your views here.
class IndexView(View):
    def get(self,request):
        """提供首页广告页面"""
        categories = get_categories()

        # 查询所有的广告类别
        contents = OrderedDict()
        content_categories = TbContentCategory.objects.all()

        # 查询出未下架的广告并排序
        for content_category in content_categories:
            contents[content_category.key] = content_category. \
                contents.filter(status=True).order_by('sequence')
        print(contents["index_3f_logo"])
        # 构造上下文
        context = {
            'categories': categories,  # 分类数据
            'contents': contents,  # 广告数据
        }
        return render(request, 'index.html', context)